//
//  MeViewController.m
//  QT
//
//  Created by 甘世清 on 2024/10/16.
//

#import "MeViewController.h"
#import "MyPictureListTableViewCell.h"
#import "MeTopView.h"
#import "MeSetUpView.h"
#import "AddPictureViewController.h"
#import "EditInfoViewController.h"
#import "MyPictureDetailsViewController.h"
#import "MyPictureViewController.h"
@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) MeTopView * meTopView;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"gengduo2") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_SetUpPopView];
    }];
    self.zx_navBar.zx_rightBtn.zx_fixImageSize = CGSizeMake(30, 30);
}

- (void)SQ_UIViewForSelfView{
    self.tableView.tableHeaderView = self.meTopView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self SQ_tableViewAddRefresh];
    WS(weakSelf);
    self.offset = 160;
    self.emptyText = SQNSLocalString(@"暂无相易?点击发表");
    [self setClickBlock:^{
        AddPictureViewController * vc = [[AddPictureViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
    [self.meTopView.headerView setTouchBlock:^(SQCustomButton * _Nonnull button) {
            EditInfoViewController *  vc =[[EditInfoViewController alloc]init];
            [weakSelf SQ_pushViewControllerFull:vc];
    }];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)SQ_Header{
    [HttpTool post:@"user/info" paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
        [UserModel SQ_updateLocal:httpModel];
        [self.meTopView SQ_Ui];
        [self.tableView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"2" forKey:@"size"];
    [HttpTool post:@"dynamic/myList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PictureModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
 
- (void)SQ_Foother{
    [self.tableView.mj_footer endRefreshing];
}

-(void)SQ_SetUpPopView{
    MeSetUpView * vc = [[MeSetUpView alloc]initWithFrame:CGRectMake(0, 0, 260, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:self.view
                                             popStyle:LSTPopStyleSmoothFromRight dismissStyle:LSTDismissStyleSmoothToRight];
    pop.hemStyle = LSTHemStyleRight;
    pop.isAvoidKeyboard = NO;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count;
}

 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyPictureListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPictureListTableViewCell"];
    if (!cell) {
        cell = [[MyPictureListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MyPictureListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray  objectAtIndex:indexPath.item];
    
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 240;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 10)];
    if (self.jsonArray.count <= 0) {
        return view;
    }
    view.backgroundColor = UIColor.whiteColor;
    UILabel * label  = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"我的相易");
    label.font = FONT_MEDIUM(16);
    label.textColor = SQColor333333;
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view).offset(5);
            make.left.equalTo(view).offset(12);
    }];
    SQCustomButton * rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(12, 12) midmargin:10];
    rightBtn.image = SQGetImage(@"");
    rightBtn.text = SQNSLocalString(@"查看更多");
    rightBtn.textColor = SQColor666666;
    rightBtn.font = FONT_REGULAR(12);
    [view addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.equalTo(view);
            make.width.mas_equalTo(70);
    }];
    WS(weakSelf);
    [rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        MyPictureViewController * vc = [[MyPictureViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQHexColor(@"#000000", 0.2);
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(0.4);
    }];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        if (self.jsonArray.count <= 0) {
            return 10;
        }
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyPictureDetailsViewController * vc = [[MyPictureDetailsViewController alloc]init];
    vc.model = [self.jsonArray objectAtIndex:indexPath.item];
    [self SQ_pushViewControllerFull:vc];
}
- (MeTopView *)meTopView{
    if (!_meTopView) {
        _meTopView = [[MeTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300)];
    }
    return _meTopView;
}
@end
